package com.yao.spider.shanghai;

import com.yao.spider.core.util.MyBatiesUtils;
import com.yao.spider.shanghai.bean.FastMail;
import com.yao.spider.shanghai.dao.FastMailDao;
import com.yao.spider.shanghai.dao.FastMailDaoImpl;
import org.apache.ibatis.session.SqlSession;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FastMailMain {
    public static void main(String[] args) {
        fastMail();
    }
    public static void fastMail() {
        try{
            // 需要读取的文件路径
            String path = "D:\\工作\\二级库\\上海数据\\address_merge_with_mobile_data.json";
            File file = new File(path);

            // 判断文件是否存在
            if (file.isFile() && file.exists()){
                FastMailDao fastMailDao = new FastMailDaoImpl();

                SqlSession sqlSession = MyBatiesUtils.getSqlSession();
                InputStreamReader read = new InputStreamReader(new FileInputStream(file));
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineText = null;
                int line = 1;
                // 按行读取文件并打印,如果需要对内容进行操作可以在这里进行
                List<FastMail> fastMailList = new ArrayList<FastMail>();
                while((lineText = bufferedReader.readLine())!=null){
                    System.out.println(line);
                    System.out.println(lineText);
                    FastMail person = new FastMail();
                    if (line > 20983) {
                        try {
                            person = JsonConvert.fastMail(lineText);
                            fastMailDao.insert(person, sqlSession);
                        } catch (Exception e) {
                            e.printStackTrace();
                            person = new FastMail();
                            person.setFailInfo(lineText);
                            fastMailDao.insert(person, sqlSession);
                        }
                    }
                    /*fastMailList.add(person);
                    while (line % 100 == 0) {
                        fastMailDao.batchInsert(fastMailList, sqlSession);
                        fastMailList = new ArrayList<FastMail>();
                    }*/
                    line++;
                }
            }
        } catch(IOException e){
            e.printStackTrace();
        }
    }
}
